/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.constant.CacheConstants;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.WorkStateEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.mapper.FxBrandPersonMapper;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.FxBrandPersonVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 品牌和c端关联表（饭友表）
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:43
 */
@Service
@Slf4j
public class FxBrandPersonServiceImpl extends ServiceImpl<FxBrandPersonMapper, FxBrandPerson> implements FxBrandPersonService {

	@Resource
	private FxBrandPersonMapper fxBrandPersonMapper;
	@Resource
	private FxWaiterExtService fxWaiterExtService;
	@Resource
	private FxRewardService fxRewardService;
	/**
	 * 缓存Api
	 */
	@Resource
	private RedisTemplate redisTemplate;


	@Override
	@Transactional
	public FxBrandPerson inviteFY(Integer inviteType,Long waiterId,Long tatId) {

		log.info("--------->邀请好友，inviteType:{},waiterId:{},tatId:{}",inviteType,waiterId,tatId);
		Long loginId = SecurityUtils.getPersonId();
		FxWaiterExt fxWaiterExtTemp = fxWaiterExtService.getById(waiterId);//不一定是当前最新的服务员信息
		FxWaiterExt fxWaiterExt = null;//前最新的服务员信息
		if(WorkStateEnum.WORK_STATE_1.getValue()!=fxWaiterExtTemp.getWorkState()){
			fxWaiterExt = fxWaiterExtService.getOne(Wrappers.lambdaQuery(FxWaiterExt.class).eq(FxWaiterExt::getLoginId,fxWaiterExtTemp.getLoginId()).eq(FxWaiterExt::getWorkState,WorkStateEnum.WORK_STATE_1.getValue()));
		}else {
			fxWaiterExt = fxWaiterExtTemp;
		}

		FxBrandPerson fxBrandPerson = this.getOne(Wrappers.lambdaQuery(FxBrandPerson.class).eq(FxBrandPerson::getBrandId,fxWaiterExt.getBrandId()).eq(FxBrandPerson::getPerId,loginId));
		//根据服务员，品牌，登录人，不存在就创建用户
		if (fxBrandPerson==null){
			//添加饭友
			fxBrandPerson = new FxBrandPerson();
			fxBrandPerson.setCompanyId(fxWaiterExt.getCompanyId());
			fxBrandPerson.setBrandId(fxWaiterExt.getBrandId());
			fxBrandPerson.setShopId(fxWaiterExt.getShopId());
			fxBrandPerson.setPerId(loginId);
			fxBrandPerson.setWaiterId(waiterId);
			fxBrandPerson.setCreateUser(loginId);
			fxBrandPerson.setCreateTime(LocalDateTime.now());
			fxBrandPerson.setInviteType(inviteType);//邀请类型0文章
			fxBrandPerson.setTatId(tatId);
			fxBrandPerson.insert();
			log.info("--------->添加饭友：{}",fxBrandPerson);
			FxReward fxReward = fxRewardService.inviteReward(fxWaiterExtTemp,fxWaiterExt,waiterId,fxBrandPerson.getId());
			if (fxReward.getMoney().compareTo(BigDecimal.ZERO) == 1) {
				redisTemplate.opsForValue().set(CacheConstants.SOUND_REMIND + waiterId, fxReward.getMoney());
			}
		}
		return fxBrandPerson;
	}

	/**
	 * 获取B端 饭友团队
	 *
	 * @param page
	 * @param fxBrandPersonVo
	 * @return
	 */
	@Override
	public IPage<FxBrandPersonVo> getFanYouTeamListPage(Page page, FxBrandPersonVo fxBrandPersonVo) {
		return fxBrandPersonMapper.getFanYouTeamListPage(page, fxBrandPersonVo);
	}

	/**
	 * 记录详情的饭友 分页查询
	 *
	 * @param page
	 * @param fxBrandPersonVo
	 * @return
	 */
	@Override
	public IPage<FxBrandPersonVo> getFanYouListPage(Page page, FxBrandPersonVo fxBrandPersonVo) {
		return fxBrandPersonMapper.getFanYouListPage(page, fxBrandPersonVo);
	}
}
